//
//  OceanDefines.h
//  OceanStaffApp
//
//  Created by Zheng on 2019/7/31.
//  Copyright © 2019 Zh. All rights reserved.
//


/** UserDefaults **/
static inline NSUserDefaults* CX_GetUserDefaults() {
    return [NSUserDefaults standardUserDefaults];
}

/** 主window **/
static inline UIWindow* CX_GetKeyWindow() {
    static UIWindow *window;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        window = [UIApplication sharedApplication].delegate.window;
    });
    return window;
}

/** 屏幕宽度 **/
static inline CGFloat CX_GetScreenWidth() {
    static CGFloat width;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        width = [UIScreen mainScreen].bounds.size.width;
    });
    return width;
}

/** 屏幕高度 **/
static inline CGFloat CX_GetScreenHeight() {
    static CGFloat height;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        height = [UIScreen mainScreen].bounds.size.height;
    });
    return height;
}

/** X比例转换值 **/
static inline CGFloat CX_GetXScale(CGFloat a) {
    static CGFloat scale;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        scale = CX_GetScreenWidth()/375.f;
    });
    return a*scale;
}

/** Y比例转换值 **/
static inline CGFloat CX_GetYScale(CGFloat a) {
    static CGFloat scale;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        scale = CX_GetScreenHeight()/667.f;
    });
    return a*scale;
}

/** 是否X型设备 **/
static inline BOOL CX_GetDeviceX() {
    CGFloat width = CX_GetScreenWidth();
    CGFloat height = CX_GetScreenHeight();
    static BOOL isDeviceX;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        BOOL isX = NO;
        if ((width == 375.f && height == 812.f) || (width == 812.f && height == 375.f)) {
            isX = YES;
        }
        BOOL isXP = NO;
        if ((width == 414.f && height == 896.f) || (width == 896.f && height == 414.f)) {
            isXP = YES;
        }
        isDeviceX = (isX || isXP);
    });
    return isDeviceX;
}

/** 状态栏高度 **/
static inline CGFloat CX_GetStatusBarHeight() {
    static CGFloat height;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        height = [UIApplication sharedApplication].statusBarFrame.size.height;
    });
    return height;
}

/** 导航栏高度 **/
static inline CGFloat CX_GetNavBarHeight() {
    static CGFloat height;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        height = CX_GetStatusBarHeight()+44.f;
    });
    return height;
}

/** 标签栏高度 **/
static inline CGFloat CX_GetTabBarHeight() {
    static CGFloat height;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        height = CX_GetDeviceX()? 83.f: 49.f;
    });
    return height;
}

/** 顶部安全区域高度 **/
static inline CGFloat CX_GetTopSafeHeight() {
    static CGFloat height;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        height = CX_GetDeviceX()? 44.f: 0.f;
    });
    return height;
}

/** 底部安全区域高度 **/
static inline CGFloat CX_GetBottomSafeHeight() {
    static CGFloat height;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        height = CX_GetDeviceX()? 34.f: 0.f;
    });
    return height;
}

/** 普通字体 **/
static inline UIFont* CX_GetNormalFont(CGFloat a) {
    UIFont *font = [UIFont systemFontOfSize:a];
    return font;
}

/** 加粗字体 **/
static inline UIFont* CX_GetBoldFont(CGFloat a) {
    UIFont *font = [UIFont boldSystemFontOfSize:a];
    return font;
}
/*
 * 判断是否是空数组 *
 static inline BOOL CX_GetArayIsEmpty(NSArray *arr) {
 if ([arr isKindOfClass:[NSNull class]] || arr == nil || arr.count == 0) {
 return YES;
 }
 return NO;
 }

 * 判断是否是空字典 *
 static inline BOOL CX_GetDicIsEmpty(NSDictionary *dic) {
 if ([dic isKindOfClass:[NSNull class]] || dic == nil || dic.allKeys.count == 0) {
 return YES;
 }
 return NO;
 }
 */

/*
 #define RGB_COLOR(_STR_,a) ([UIColor colorWithRed:[[NSString stringWithFormat:@"%lu", strtoul([[_STR_ substringWithRange:NSMakeRange(1, 2)] UTF8String], 0, 16)] intValue] / 255.0 green:[[NSString stringWithFormat:@"%lu", strtoul([[_STR_ substringWithRange:NSMakeRange(3, 2)] UTF8String], 0, 16)] intValue] / 255.0 blue:[[NSString stringWithFormat:@"%lu", strtoul([[_STR_ substringWithRange:NSMakeRange(5, 2)] UTF8String], 0, 16)] intValue] / 255.0 alpha:a])
 */

/** 16进制颜色 **/
static inline UIColor* CX_GetColorHex(NSString *h, CGFloat a) {

    CGFloat r,g,b;
    r = ([[NSString stringWithFormat:@"%lu", strtoul([[h substringWithRange:NSMakeRange(1, 2)] UTF8String], 0, 16)] intValue]) / 255.0;
    g = ([[NSString stringWithFormat:@"%lu", strtoul([[h substringWithRange:NSMakeRange(3, 2)] UTF8String], 0, 16)] intValue]) / 255.0;
    b = ([[NSString stringWithFormat:@"%lu", strtoul([[h substringWithRange:NSMakeRange(5, 2)] UTF8String], 0, 16)] intValue]) / 255.0;

    UIColor *color = [UIColor colorWithRed:r green:g blue:b alpha:a];
    return color;
}

/** rgb颜色 **/
static inline UIColor* CX_GetColorRGB(CGFloat r, CGFloat g, CGFloat b, CGFloat a) {
    UIColor *color = [UIColor colorWithRed:r/255.f green:g/255.f blue:b/255.f alpha:a];
    return color;
}

/** 随机颜色 **/
static inline UIColor* CX_GetColorRandom() {
    UIColor *color = CX_GetColorRGB(arc4random()%256, arc4random()%256, arc4random()%256, 1);
    return color;
}


/** 常用颜色 **/
static inline UIColor* CX_GetColorTint() {
    static UIColor *color;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        color = CX_GetColorHex(@"#00C1B6", 1);
    });
    return color;
}

static inline UIColor* CX_GetColorRed() {
    static UIColor *color;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        color = CX_GetColorHex(@"#FF4500", 1);
    });
    return color;
}

static inline UIColor* CX_GetColorTitle() {
    static UIColor *color;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        color = CX_GetColorHex(@"#444444", 1);
    });
    return color;
}

static inline UIColor* CX_GetColorSubTitle() {
    static UIColor *color;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        color = CX_GetColorHex(@"#4A4A4A", 1);
    });
    return color;
}

static inline UIColor* CX_GetColorLine() {
    static UIColor *color;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        color = CX_GetColorHex(@"#F5F5F5", 1);
    });
    return color;
}

static inline UIColor* CX_GetColorLightGray() {
    static UIColor *color;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        color = CX_GetColorHex(@"#999999", 1);
    });
    return color;
}

static inline UIColor* CX_GetColorGray() {
    static UIColor *color;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        color = CX_GetColorHex(@"#666666", 1);
    });
    return color;
}


#ifndef OceanDefines_h
#define OceanDefines_h


#define Available_Block(b) ((b == nil)? nil: b)

#ifdef DEBUG
#define NSLog(...) NSLog(@"%s 第%d行 \n %@",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define NSLog(...)
#endif


#ifndef weakify
#if DEBUG
#if __has_feature(objc_arc)
#define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
#else
#define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
#endif
#else
#if __has_feature(objc_arc)
#define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
#else
#define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
#endif
#endif
#endif

#ifndef strongify
#if DEBUG
#if __has_feature(objc_arc)
#define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
#else
#define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
#endif
#else
#if __has_feature(objc_arc)
#define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
#else
#define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
#endif
#endif
#endif


#endif /* OceanDefines_h */
